/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package privileges.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import login.bean.PrincipalPageBean;
import privileges.business.UserBusiness;

/**
 *
 * @author Javier
 */
@ManagedBean
@RequestScoped
public class CreateUserBean implements Serializable{
    private String nameUser;
    private String host;
    private String password;
    private String retypePassword;
    private String maxQueriesPerHour;
    private String maxUpdatesPerHour;
    private String maxConnectionsPerHour;
    private String maxUserConnections;
    private boolean checkData;
    private boolean checkStructure;
    private boolean checkAdmin;
    private List<String> selectedStructureOptions;
    private List<String> selectedDataOptions;
    private List<String> selectedAdministrationOptions;


    public CreateUserBean() {
        this.checkData = false;
        this.checkAdmin = false;
        this.checkStructure = false;
        this.maxQueriesPerHour = "0";
        this.maxUpdatesPerHour = "0";
        this.maxConnectionsPerHour = "0";
        this.maxUserConnections = "0";
    }

    public boolean isCheckAdmin() {
        return checkAdmin;
    }

    public void setCheckAdmin(boolean checkAdmin) {
        this.checkAdmin = checkAdmin;
    }

    public boolean isCheckStructure() {
        return checkStructure;
    }

    public void setCheckStructure(boolean checkStructure) {
        this.checkStructure = checkStructure;
    }

    public boolean isCheckData() {
        return checkData;
    }

    public void setCheckData(boolean checkData) {
        this.checkData = checkData;
    }
    
    public String getNameUser() {
        return nameUser;
    }

    public String getMaxQueriesPerHour() {
        return maxQueriesPerHour;
    }

    public void setMaxQueriesPerHour(String maxQueriesPerHour) {
        this.maxQueriesPerHour = maxQueriesPerHour;
    }

    public String getMaxUpdatesPerHour() {
        return maxUpdatesPerHour;
    }

    public void setMaxUpdatesPerHour(String maxUpdatesPerHour) {
        this.maxUpdatesPerHour = maxUpdatesPerHour;
    }

    public String getMaxConnectionsPerHour() {
        return maxConnectionsPerHour;
    }

    public void setMaxConnectionsPerHour(String maxConnectionsPerHour) {
        this.maxConnectionsPerHour = maxConnectionsPerHour;
    }

    public String getMaxUserConnections() {
        return maxUserConnections;
    }

    public void setMaxUserConnections(String maxUserConnections) {
        this.maxUserConnections = maxUserConnections;
    }

    public List<String> getSelectedStructureOptions() {
        return selectedStructureOptions;
    }

    public void setSelectedStructureOptions(List<String> selectedStructureOptions) {
        this.selectedStructureOptions = selectedStructureOptions;
    }

    public List<String> getSelectedDataOptions() {
        return selectedDataOptions;
    }

    public void setSelectedDataOptions(List<String> selectedDataOptions) {
        this.selectedDataOptions = selectedDataOptions;
    }

    public List<String> getSelectedAdministrationOptions() {
        return selectedAdministrationOptions;
    }

    public void setSelectedAdministrationOptions(List<String> selectedAdministrationOptions) {
        this.selectedAdministrationOptions = selectedAdministrationOptions;
    }

    public void setNameUser(String nameUser) {
        this.nameUser = nameUser;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRetypePassword() {
        return retypePassword;
    }

    public void setRetypePassword(String retypePassword) {
        this.retypePassword = retypePassword;
    }
    
    public void changeCheckData(){
        if(checkData){
            List<String> selectedDataOptionsTemp = new ArrayList<>();
            selectedDataOptionsTemp.add("SELECT");
            selectedDataOptionsTemp.add("INSERT");
            selectedDataOptionsTemp.add("UPDATE");
            selectedDataOptionsTemp.add("DELETE");
            selectedDataOptionsTemp.add("FILE");
            selectedDataOptions = selectedDataOptionsTemp;
        }else{
            selectedDataOptions = new ArrayList<>();
        }
    }
        
    public void changeCheckAdmin(){
        if(checkAdmin){
            List<String> selectedAdminOptionsTemp = new ArrayList<>();
            selectedAdminOptionsTemp.add("GRANT OPTION");
            selectedAdminOptionsTemp.add("SUPER");
            selectedAdminOptionsTemp.add("PROCESS");
            selectedAdminOptionsTemp.add("RELOAD");
            selectedAdminOptionsTemp.add("SHUTDOWN");
            selectedAdminOptionsTemp.add("SHOW DATABASES");
            selectedAdminOptionsTemp.add("LOCK TABLES");
            selectedAdminOptionsTemp.add("REFERENCES");
            selectedAdminOptionsTemp.add("REPLICATION CLIENT");
            selectedAdminOptionsTemp.add("REPLICATION SLAVE");
            selectedAdminOptionsTemp.add("CREATE USER");
            selectedAdministrationOptions = selectedAdminOptionsTemp;
        }else{
            selectedAdministrationOptions = new ArrayList<>();
        }
    }
    
   public void changeCheckStructure(){
        if(checkStructure){
            List<String> selectedStructureOptionsTemp = new ArrayList<>();
            selectedStructureOptionsTemp.add("CREATE");
            selectedStructureOptionsTemp.add("ALTER");
            selectedStructureOptionsTemp.add("INDEX");
            selectedStructureOptionsTemp.add("DROP");
            selectedStructureOptionsTemp.add("CREATE TEMPORARY TABLES");
            selectedStructureOptionsTemp.add("SHOW VIEW");
            selectedStructureOptionsTemp.add("CREATE ROUTINE");
            selectedStructureOptionsTemp.add("ALTER ROUTINE");
            selectedStructureOptionsTemp.add("EXECUTE");
            selectedStructureOptionsTemp.add("CREATE VIEW");
            selectedStructureOptionsTemp.add("EVENT");
            selectedStructureOptionsTemp.add("TRIGGER");
            selectedStructureOptions = selectedStructureOptionsTemp;
        }else{
            selectedDataOptions = new ArrayList<>();
        }
    }
    
    public String createUser(){
        FacesContext context = FacesContext.getCurrentInstance(); 
        if(nameUser.equals("") || host.equals("")){
            context.addMessage(null, new FacesMessage("Error!!", "Debe ingresar nombre de usuario y host"));
            return "";
        }  
        else{
        if(password.equals(retypePassword)){
            try {
                new UserBusiness().createUser(nameUser,host,password
                        ,selectedStructureOptions,selectedDataOptions
                        ,selectedAdministrationOptions,maxUserConnections
                        ,maxConnectionsPerHour,maxUpdatesPerHour,maxQueriesPerHour);
                context.addMessage(null, new FacesMessage("Exito", "Usuario Creado Correctamente"));
                PrincipalPageBean.centerPage = "usuarios";
                return "principalPage.xhtml?faces-redirect=true";
            } catch (SQLException ex) {
                context.addMessage(null, new FacesMessage("Error!!", ex.getLocalizedMessage()));
                return "";
            }
        }
        else
            context.addMessage(null, new FacesMessage("Error!!", "Las claves no Coinciden"));
        return "";
    }  
   }  
}
